﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using CMISLibrary;
using System.Globalization;
using System.Data.OracleClient;
using BCNPCObject.DB;

namespace BCNPCObject
{
    public class cls_CN_PHIEU_THU_Controller
    {
        #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private cls_CN_PHIEU_THU_Info info;
        private CN_PHIEU_THU infoNew;
        private List<CN_PHIEU_THU> lstInfo;
        #endregion

        #region   Constructor

        public cls_CN_PHIEU_THU_Controller()
        {
            info = new cls_CN_PHIEU_THU_Info();
            lstInfo = new List<CN_PHIEU_THU>();
            infoNew = new CN_PHIEU_THU();
        }
        public cls_CN_PHIEU_THU_Controller(cls_CN_PHIEU_THU_Info Info)
        {
            this.info = Info;
        }
        public cls_CN_PHIEU_THU_Controller(List<CN_PHIEU_THU> LstInfo, int i)
        {
            this.lstInfo = LstInfo;
        }
        public cls_CN_PHIEU_THU_Controller(CN_PHIEU_THU InfoNew, int i, int j)
        {
            this.infoNew = InfoNew;
        }
        #endregion

        #region   Properties

        public cls_CN_PHIEU_THU_Info pInfor
        {
            get { return info; }
            set { info = value; }
        }
        public List<CN_PHIEU_THU> pLstInfo
        {
            get { return lstInfo; }
            set { lstInfo = value; }
        }
        public CN_PHIEU_THU Infornew
        {
            get { return Infornew; }
            set { Infornew = value; }
        }
        
        #endregion

        #region Method DũngNT
        public long getSoPThu()
        {
            try
            {
                long lngSeqLog = CMIS2.DB.ExecuteCommand("SELECT SEQ_CN_PHIEU_THU.NEXTVAL FROM DUAL");
                return lngSeqLog;
            }
            catch
            {
                return -1;
            }
        }
        public bool insert_PhieuThu( ref long _SO_PHTHU)
        {
            long _ID = getSoPThu();
            _SO_PHTHU = _ID;
            if (_ID == -1) return false;
            try
            {
                CN_PHIEU_THU obj = new CN_PHIEU_THU()
                {
                    SO_PHTHU = _ID,
                    MA_DVIQLY = info.MA_DVIQLY,
                    MA_KHANG = info.MA_KHANG,
                    MA_KHTT = info.MA_KHTT,
                    MA_PTTT = info.MA_PTTT,
                    MA_TNGAN = info.MA_TNGAN,
                    SO_TIEN = info.SO_TIEN,
                    TIEN_THUA = info.TIEN_THUA,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGUOI_SUA = info.NGUOI_SUA,
                    NGAY_TAO = info.NGAY_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_NOP = info.NGAY_NOP,
                    SO_BBQTOAN = info.SO_BBQTOAN,
                    TSO_HDON = info.TSO_HDON
                };
                CMIS2.DB.CN_PHIEU_THU.InsertOnSubmit(obj);
                //CMIS2.DB.SubmitChanges();
                //CMIS2.ResetDB();
                return true;
            }
            catch
            {
                //CMIS2.ResetDB(); 
                return false;
            }
        }
        public bool delete_PhieuThu(string maDViQLy, long SO_PHTHU)
        {
            try
            {
                CN_PHIEU_THU objCN_PHIEU_THU = CMIS2.DB.CN_PHIEU_THU.Single(p => p.MA_DVIQLY == maDViQLy && p.SO_PHTHU == SO_PHTHU);
                if (objCN_PHIEU_THU != null)
                {
                    CMIS2.DB.CN_PHIEU_THU.DeleteOnSubmit(objCN_PHIEU_THU);
                    return true;
                }
                return true;

            }
            catch
            {
                //CMIS2.ResetDB();
                return false;
            }
        }
        public DataSet getPhieuThu(string strMaDViQLy, string strMaKHTT, string strMaTNgan, string strLoaiPThu, long lngSoPhieu, DateTime? dtNgayThu, DateTime? dtDenNgay, ref string strError)
        {
            //Dùng khi tìm kiếm phiếu thu (PTHU) trong chức năng Thu và chấm nợ tại quầy
            try
            {
                ds.Tables.Clear();
                var pthu = (from i in CMIS2.DB.CN_PHIEU_THU
                            where i.MA_DVIQLY == strMaDViQLy
                            && i.SO_BBQTOAN == 0
                                //&& (strNgayThu != "01/01/3000" && i.NGAY_NOP == dtmNgayThu || strNgayThu == "01/01/3000")
                            && ((lngSoPhieu > 0 && i.SO_PHTHU == lngSoPhieu) || lngSoPhieu == -1)
                            && ((strMaKHTT.Trim().Length > 0 && i.MA_KHTT == strMaKHTT) || strMaKHTT.Trim().Length == 0)
                            && ((strMaTNgan.Trim().Length > 0 && i.MA_TNGAN == strMaTNgan) || strMaTNgan.Trim().Length == 0)
                            select new
                            {
                                i.MA_DVIQLY,
                                i.MA_KHTT,
                                i.NGAY_NOP,// =  i.NGAY_NOP.ToString("dd/MM/yyyy"),
                                i.MA_TNGAN,
                                i.MA_PTTT,
                                i.SO_PHTHU,
                                i.TIEN_THUA,
                                i.SO_TIEN,
                                i.TSO_HDON
                            }).ToList();
                if (pthu == null || pthu.Count == 0)
                {
                    strError = "Không tìm thấy thông tin phiếu thu";
                    return null;
                }
                if (dtNgayThu != null && dtDenNgay != null)
                    pthu = pthu.Where(c => c.NGAY_NOP.Date <= dtNgayThu.Value.Date && c.NGAY_NOP.Date >= dtDenNgay.Value.Date).ToList();
                if (pthu == null || pthu.Count == 0)
                {
                    strError = "Không tìm thấy thông tin phiếu thu";
                    return null;
                }
                long[] arrSoPThu = pthu.Select(c => c.SO_PHTHU).Distinct().ToArray();
                //*********************************************************************                
                List<CN_HDON_PHTHU> p = new List<CN_HDON_PHTHU>();
                while (arrSoPThu.Length > 0)
                {
                    long[] arrTemp = arrSoPThu.Take(1000).ToArray();
                    var lstTemp = (from a in CMIS2.DB.CN_HDON_PHTHU
                                   where a.MA_DVIQLY == strMaDViQLy
                                   && arrTemp.Contains(a.SO_PHTHU)
                                   select a).ToList();
                    if (lstTemp != null && lstTemp.Count > 0)
                        p.AddRange(lstTemp);
                    arrSoPThu = arrSoPThu.Skip(1000).ToArray();
                }
                if (p == null || p.Count == 0)
                {
                    strError = "Không tìm thấy thông tin hóa đơn";
                    return null;
                }
                //*********************************************************************
                long[] arrSoPThuHDon = p.Select(c => c.SO_PHTHU).ToArray();
                pthu = (from a in pthu
                        where (from b in arrSoPThuHDon where b == a.SO_PHTHU select b).ToList().Count > 0
                        select a).Distinct().ToList();
                //**********************************************************************
                string[] arrMaTNgan = pthu.Select(c => c.MA_TNGAN).Distinct().ToArray();
                var tngan = (from a in CMIS2.DB.D_TNGAN_VIEN
                             where a.MA_DVIQLY==strMaDViQLy && a.TINH_TRANG==1 && arrMaTNgan.Contains(a.MA_TNGAN)
                             select new
                             {
                                 a.MA_TNGAN,
                                 a.TEN_TNGAN
                             }).Distinct().ToList();
                var kq = from a in pthu
                         join c in tngan on a.MA_TNGAN equals c.MA_TNGAN
                         select new
                         {
                             a.MA_DVIQLY,
                             a.MA_KHTT,
                             a.MA_PTTT,
                             a.NGAY_NOP,
                             a.SO_TIEN,
                             c.TEN_TNGAN,
                             a.SO_PHTHU,
                             a.MA_TNGAN
                         };
                DataTable dt = new DataTable();
                dt = Utility.LINQToDataTable(kq);
                dt.TableName = "CN_HDON_PHTHU";
                ds.Tables.Add(dt);
                return ds;
            }
            catch (Exception ex)
            {
                strError = "Lỗi lớp Object: " + ex.Message;
                return null;
            }
        }

        #endregion
    }
}
